/*
 * Copyright 2009 Tran Nhut Trung.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package org.eclipse.core.databinding.conversion.gwt;

import com.google.gwt.i18n.client.NumberFormat;

import org.eclipse.core.databinding.conversion.Converter;

public class StringToNumberConverter extends Converter{
  private Class toType;
  private NumberFormat numberFormat;

  public StringToNumberConverter(Class toType, NumberFormat numberFormat) {
    super(String.class, toType);

    this.toType = toType;
    if (numberFormat == null){
      numberFormat = Converters.DEFAULT_NUMBER_FORMAT;
    }
    this.numberFormat = numberFormat;
  }

  public Object convert(Object fromObject) {
    String fromString = (String)fromObject;
    fromString = fromString.trim();
    if ((fromString == null) || (fromString.length() == 0)){
      return null;
    }
    double value = numberFormat.parse((String)fromObject);
    Double result = new Double(value);
    
    if (toType == Double.class){
      if ((value < Double.MIN_VALUE) || (value > Double.MAX_VALUE)){
        throw new IllegalArgumentException("Out of double range [" + fromObject + "]");
      }
      return result.doubleValue();
    }
    if (toType == Float.class){
      if ((value < Float.MIN_VALUE) || (value > Float.MAX_VALUE)){
        throw new IllegalArgumentException("Out of float range [" + fromObject + "]");
      }
      return result.floatValue();
    }

    // check for integer value
    if (value % 1 != 0){
      throw new IllegalArgumentException("Could not convert " + fromObject + " to integer");
    }

    if (toType == Long.class){
      if ((value < Long.MIN_VALUE) || (value > Long.MAX_VALUE)){
        throw new IllegalArgumentException("Out of long integer range [" + fromObject + "]");
      }
      return result.longValue();
    }
    if (toType == Integer.class){
      if ((value < Integer.MIN_VALUE) || (value > Integer.MAX_VALUE)){
        throw new IllegalArgumentException("Out of integer range [" + fromObject + "]");
      }
      return result.intValue();
    }
    if (toType == Short.class){
      if ((value < Short.MIN_VALUE) || (value > Short.MAX_VALUE)){
        throw new IllegalArgumentException("Out of short integer range [" + fromObject + "]");
      }
      return result.shortValue();
    }
    if (toType == Byte.class){
      if ((value < Byte.MIN_VALUE) || (value > Byte.MAX_VALUE)){
        throw new IllegalArgumentException("Out of byte range [" + fromObject + "]");
      }
      return result.byteValue();
    }

    throw new IllegalArgumentException("Could not convert [" + fromObject + "] to type [" + toType + "]");
  }

  public static StringToNumberConverter toLong(NumberFormat numberFormat) {
    return new StringToNumberConverter(Long.class, numberFormat);
  }

  public static StringToNumberConverter toInteger(NumberFormat numberFormat) {
    return new StringToNumberConverter(Integer.class, numberFormat);
  }

  public static StringToNumberConverter toShort(NumberFormat numberFormat) {
    return new StringToNumberConverter(Short.class, numberFormat);
  }

  public static StringToNumberConverter toByte(NumberFormat numberFormat) {
    return new StringToNumberConverter(Byte.class, numberFormat);
  }

  public static StringToNumberConverter toDouble(NumberFormat numberFormat) {
    return new StringToNumberConverter(Double.class, numberFormat);
  }

  public static StringToNumberConverter toFloat(NumberFormat numberFormat) {
    return new StringToNumberConverter(Float.class, numberFormat);
  }
}
